﻿using System.Linq;
using System.Xml.Linq;
using NUnit.Framework;
using OpenWaves.EPiServer.Localization.Model;
using System.Xml.XPath;
using OpenWaves.EPiServer.Localization.UnitTests.Resources.LanguageFiles;

namespace OpenWaves.EPiServer.Localization.UnitTests
{
    [TestFixture]
    public class XmlMergeTests
    {
        [Test]
        public void TestMergingLanguageBranches()
        {
            const string A =
@"<languages><language name=""English"" id=""en"">
    <frontPage>
        <heading>heading_en</heading>
        <sub>sub_en</sub>
    </frontPage>
    <properties>
        <property name=""teaser""><body>body_1</body></property>        
    </properties>
    <properties>
        <property name=""teaser""><body>body_3</body></property>
    </properties>
</language></languages>";

            const string B =
@"<languages><language name=""Norsk"" id=""no"">
    <frontPage>
        <heading>heading_no</heading>
        <intro>intro_no</intro>
    </frontPage>   
</language></languages>";
               
            var result = LanguageXmlMerge.Merge(XDocument.Parse(A), XDocument.Parse(B));


            AssertNodeEqual("heading_en", "/languages/language/frontPage/heading", result);
            AssertNodeEqual("sub_en", "/languages/language/frontPage/sub", result);
            AssertNodeEqual("intro_no", "/languages/language/frontPage/intro", result);
            AssertNodeEqual("body_1", "/languages/language/properties/property/body", result);

        }

        private static void AssertNodeEqual(string expected, string xPath, XDocument document)
        {
            var elements = document.XPathSelectElements(xPath).ToList();
            Assert.AreEqual(1, elements.Count(), xPath);
            Assert.AreEqual(expected, elements.Single().Value, xPath);
        }

        [Test]
        public void TestMergingPropertyNames2()
        {
            const string C = @"<languages>
  <language name=""English"" id=""en"">
    <pagetypes>
      <common>       
        <property name=""TeaserText"">
          <caption>Teaser text</caption>
          <help>Can be used to display a text other than the description when the page is dropped in a content area</help>
        </property>
        <property name=""TeaserText"">
          <caption>Text</caption>
          <help></help>
        </property>      
      </common>
    </pagetypes>    
  </language>  
</languages>";

            var result = LanguageXmlMerge.Merge(XDocument.Parse("<languages></languages>"), XDocument.Parse(C));

            AssertNodeEqual("Teaser text", "/languages/language/pagetypes/common/property[@name='TeaserText']/caption", result);

        }


        [Test]
        public void TestMergingPropertyNames()
        {
            var result = LanguageXmlMerge.Merge(XDocument.Parse(TestResources.Channels), XDocument.Parse(TestResources.PropertyNames));

            AssertNodeEqual("Teaser text", "/languages/language/pagetypes/common/property[@name='TeaserText']/caption", result);

        }
    }
}